package app;/**
 * @description:
 * @authoer: Max Woods
 * @create:2018/6/29 17:07
 **/


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import nimble.json.FastjsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @auther: Max Woods
 * @create:2018/6/29 17:07
 **/
public class DataGen2  {

    private static final Logger logger = LoggerFactory.getLogger(DataGen2.class);

    public static void main(String[] args) throws Exception {
        String filePath = "D:/首席收藏拍品链接.xls";
        String jsonFile="D:/首席收藏拍品链接.json";
        WebClient webClient = new WebClient();
        InterceptWebConnection webConnection=new InterceptWebConnection(webClient);
        //webClient.setWebConnection(webConnection);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setPrintContentOnFailingStatusCode(false);
        FileInputStream fis = new FileInputStream(filePath);
        final HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        JSONArray ja=new JSONArray();
        GoodsData goodsData;
        //getData(webClient,"http://www.huayicang.com/hxgq/xpai/goods_18-0611-1-163.html");
        int total=sheet.getLastRowNum();
        logger.info("导出数据最大行号：{}",total);
        int fileid=8;
        int section=1000;
        for (int i = fileid*section; i < (fileid+1)*section; i++) {
            String url = sheet.getRow(i).getCell(0).getStringCellValue();
            logger.info("url[{}]:{}", i, url);
            try {
                goodsData = getData(webClient, url);
                ja.add(JSON.toJSON(goodsData));
                logger.info("产品数据:{}", FastjsonUtils.toJSONString(goodsData));
            }
            catch (Exception e)
            {
                logger.error("====================提取数据异常:{}",url);
            }
        }
        workbook.close();
        fis.close();
        webClient.close();
        writeTextFile(jsonFile,ja);
    }

    public static GoodsData getData(WebClient webClient, String url) throws Exception {
        logger.info("提取拍品数据：{}", url);
        GoodsData goodsData=new GoodsData();
        HtmlPage htmlPage = null;
        htmlPage = webClient.getPage(url);
        HtmlDivision articleNode = (HtmlDivision) htmlPage.getByXPath("//html//body//div[@id='body']//div//div[@class='left articleBox']//div[@class='Article']").get(0);
        HtmlDivision head=(HtmlDivision)articleNode.getByXPath("div[@class='artHead']").get(0);
        HtmlHeading2 nameNode = (HtmlHeading2) head.getByXPath("h2").get(0);
        String name = nameNode.getFirstChild().getTextContent();
        char[] a=new char[]{0xa0,0xa0};
        name=StringUtils.replace(name,new String(a),StringUtils.SPACE);
        name=StringUtils.substringAfter(name,StringUtils.SPACE);

        //
        HtmlTable tableNode=(HtmlTable)htmlPage.getByXPath("//html//body//div[@id='body']//table//tbody//tr//td//table[@class='details left']").get(0);
        HtmlTableRow descNode = (HtmlTableRow) tableNode.getByXPath("tbody//tr").get(4);
        HtmlTableDataCell descText = (HtmlTableDataCell) descNode.getByXPath("td[@class='f12']").get(0);
        String desc = descText.getTextContent();
        //
        goodsData.setName(name);
        goodsData.setDesc(desc);
        //
        HtmlDivision imageListDiv=(HtmlDivision)htmlPage.getByXPath("//html//body//div//div[@class='right rightBar']//div[@class='bx-wrapper']//div[@class='bx-viewport']//div[@class='rightScroll']").get(0);
        List<DomNode> imagesNode = imageListDiv.getByXPath("div[@class='slide']");
        for (DomNode node : imagesNode) {
            HtmlImage imageNode=(HtmlImage) node.getByXPath("p[@class='rSrcollList']//span//img").get(0);
            String imgscript=imageNode.getAttributes().getNamedItem("onclick").getTextContent();
            String imgLink=StringUtils.substringBetween(imgscript,"'");
            goodsData.getImages().add(imgLink);
        }
        return goodsData;
    }

    private static void writeTextFile(String jsonFile,JSONArray ja) throws Exception
    {
        File writename = new File(jsonFile); // 相对路径，如果没有则要建立一个新的output。txt文件
        writename.createNewFile(); // 创建新文件
        BufferedWriter out = new BufferedWriter(new FileWriter(writename));
        out.write(ja.toString()); // \r\n即为换行
        out.flush(); // 把缓存区内容压入文件
        out.close(); // 最后记得关闭文件
    }
}
